<?php
namespace src\logic;
use classlib\core as C;
class lpaypal {
	/**
	 * 直接通过PayPal充值支付
	 * @param $token  string
	 * @param $amount int
	 * @param $orderId  string
	 * @param $quantity int
	 * @param $itemid int
	 * @param $itemname int
	 * @param $currency int
	 * @return array
	 */
	public static function charge($token, $amount, $orderId, $quantity=1, $itemid=1000, $itemname='PocketLive PayCenter',  $currency='CAD') {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'amount'=>$amount, 'currency'=>$currency, 'itemId'=>$itemid, 'itemName'=>$itemname,
			'quantity'=>$quantity, 'orderId'=>$orderId, 'spotCd'=>lsocial::SpotCd,  'ts'=>lsocial::microsec());
		$qrystr['hs'] = md5($conf['skey'].$qrystr['ts'].$amount.$itemid.$quantity);
		$url    = $conf['psapi'].'/payment/paypal/checkout/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'POST');
		return $res;
	}
	
	/**
	 * 查询一笔订单交易的状态，判断是否允许重复支付
	 * @param unknown $token
	 * @param unknown $transid
	 * @return array
	 */
	public static function state($token, $transid) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'transactionId'=>$transid, 'spotCd'=>lsocial::SpotCd);
		$url    = $conf['psapi'].'/payment/paypal/txn/'.$transid.'/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'GET');
		return $res;
	}
	
	/**
	 * 绑定账号
	 * @param $token  string
	 * @param $email string
	 * @param $firstName  string
	 * @param $lastName string
	 * @return array
	 */
	public static function account($token, $email, $firstName, $lastName) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'email'=>$email, 'firstName'=>$firstName, 'lastName'=>$lastName, 'spotCd'=>lsocial::SpotCd);
		$url    = $conf['psapi'].'/payment/paypal/checkaccount/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'GET');
		return $res;
	}
	
	/**
	 * 转账
	 * @param $token  string
	 * @param $paypalAccountId int
	 * @param $amount  double
	 * @param $plivePayoutId string
	 * @param $currency String
	 * @return array
	 */
	public static function payout($token, $email, $amount, $plivePayoutId) {
		$conf   = C\config::load('psocial', false);
		$qrystr = array('token'=>$token, 'email'=>$email, 'amount'=>$amount, 'plivePayoutId'=>$plivePayoutId,
				'currency'=>$conf['currency'], 'spotCd'=>lsocial::SpotCd, 'ts'=>lsocial::microsec());
		$qrystr['hs'] = md5($conf['skey'].$qrystr['ts'].$amount.$plivePayoutId.$email);
		$url    = $conf['psapi'].'/payment/paypal/payout/?'.http_build_query($qrystr);
		$res    = lsocial::requst($url, 'POST');
		return $res;
	}
}